<?php
namespace app\common\model\approval;
use app\api\controller\Pub;
use app\common\model\user\User;
use app\common\model\power\Rolepower;
use app\common\model\company\Contract;
use app\common\model\orderhawa\HawaList;
use think\Model;

//审批角色列表
class Approvalrole extends Model
{
    protected $table = 'approval_role';
    protected $pk    = 'Id';
    
    const STATEINT  = 1; //初始化 进行中
    const STATEING  = 1; //规则启用
    const STATESTOP   = 2; //规则暂停

    /**
     * 根据type 获取 初始化完成的 审批规则
     * 返回规则信息 以及规则的id
     * @param  [type] $data [description]
     * @return [type]       [description]
     */
    public static function getrole($data){
        $sb = new Approvalrole;
        $data = $sb->where('Type',$data['Type'])->where('State',Approvalrole::STATEING)->find();
        if(empty($data)){
            return moerr('暂未设置此类型订单的审批规则');
        }
        $rdata = json_decode($data['Rules'],true);
        foreach ($rdata['Rules'] as $key => $value) {
            foreach ($value['UserIds'] as $vk => $vv) {
                $rdata['Rules'][$key]['list'][] = array('UserId' => $vv,'Note'=>'','Img'=>'','Time'=>'','State'=>1);
            }
        }
        return ['data'=>$rdata,'Id'=>$data->Id];
    }

    /**
     * 根据用户id 显示所有管理的role
     * @return [type] [description]
     */
    public static function user_get_manager_id(){
        $userid = Pub::userid();
        $sb = new Approvalrole;
        $data = $sb->where('State',Approvalrole::STATEING)->select();
        $idlist= [];
        foreach ($data as $key => $value) {
            $rdata = json_decode($value['Rules'],true);
            foreach ($rdata['Rules'] as $vk => $vv) {
                if(in_array($userid,$vv['UserIds'])){
                    $idlist[] = $value['Id'];
                    break;
                }
            }
        }
        $idlist = array_unique($idlist);
        return implode(',', $idlist);;
    } 
    
}
